package com.lysj.bhp.withdraw.controller;

import com.lysj.bhp.util.web.ParamUtil;
import com.lysj.bhp.util.web.Resp;
import com.lysj.bhp.withdraw.base.WithdrawContent;
import com.lysj.bhp.withdraw.domain.DayLimit;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("admin/day_limit")
public class DayLimitController extends WithdrawContent {


    @GetMapping("list")
    public Resp list(DayLimit model) {
        Specification specification = ParamUtil.createSpecification(model);
        List<DayLimit> dayLimitList = dayLimitRepository.findAll(specification, new Sort(Sort.Direction.ASC, "dayLimitId"));
        return new Resp(dayLimitList);
    }


    @PostMapping("/edit")
    public Resp edit(DayLimit model) {
        if (ParamUtil.isBlack(model.getDayLimitId())) {
            return new Resp(Resp.PARAM_ERROR, "请选择币种");
        }
        DayLimit dayLimit = dayLimitRepository.findOne(model.getDayLimitId());
        if (null == dayLimit) {
            return new Resp(Resp.PARAM_ERROR, "查无此币种");
        }
        if(null == model.getAmount()){
            return new Resp(Resp.PARAM_ERROR,"每日限额数量不能为空");
        }

        if (model.getAmount().compareTo(BigDecimal.ZERO) != 1 ) {
            return new Resp(Resp.PARAM_ERROR, "每日限额数量必须大于0");
        }
        dayLimit.setAmount(model.getAmount());
        dayLimitRepository.save(dayLimit);
        return new Resp("修改成功");
    }


}
